//
//  StatusCell.swift
//  sinaOnceAgain
//
//  Created by 王相超 on 16/2/20.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    
    
    var bottomViewTopConstaint: Constraint?
    
    //添加模型属性
    var status: Status? {
        didSet{
            
            //设置原创微博的数据模型
            statusOringinalView.status = status
    
            
            //卸载老的约束对象
            self.bottomViewTopConstaint?.uninstall()
            //根据数据调整约束关系
            if let re = status?.retweeted_status {
                
                //转发微博
                
                //设置转发微博设置数据模型
                statusRetweetedView.retweetStatus = re
                
                statusCellBottonView.snp_makeConstraints { (make) -> Void in
                    
                    self.bottomViewTopConstaint = make.top.equalTo(statusRetweetedView.snp_bottom).constraint}
                
                //显示转发微博视图
                statusRetweetedView.hidden = false
                
            }else{
                //原创微博
                statusCellBottonView.snp_makeConstraints { (make) -> Void in
                    
                    self.bottomViewTopConstaint = make.top.equalTo(statusOringinalView.snp_bottom).constraint}
                
                //隐藏转发微博视图
                statusRetweetedView.hidden = true
            }
        }
    }
    
    //xib加载的视图  才会走这个视图
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    //cell的默认的构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //完成自定义的操作
        setupUI()
        
        //设置cell 选中样式
        selectionStyle = .None
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加子视图
    private func setupUI() {
        
        //cell中 需要使用 contentview
        contentView.addSubview(statusOringinalView)
        contentView.addSubview(statusCellBottonView)
        contentView.addSubview(statusRetweetedView)
        
        //设置约束
        statusOringinalView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.top.equalTo(contentView.snp_top)
        }
        
        statusRetweetedView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(statusOringinalView.snp_bottom)
            make.left.equalTo(statusOringinalView.snp_left)
            make.right.equalTo(statusOringinalView.snp_right)
            
        }
        
        statusCellBottonView.snp_makeConstraints { (make) -> Void in
            
            self.bottomViewTopConstaint = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            //设置高度
            make.height.equalTo(35)
        }
        
        //给contentview添加约束
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            
            //关键约束
            make.bottom.equalTo(statusCellBottonView.snp_bottom)
        }
        
    }
    
    
    //懒加载子视图
    private lazy var statusOringinalView: StatusCellOriginalView = StatusCellOriginalView()
    
    //转发微博部分视图
    private lazy var statusRetweetedView: StatusRetweetedView = StatusRetweetedView()
    
    //添加底部视图
    private lazy var statusCellBottonView: StatusCellBottomView = StatusCellBottomView()
    
    
}
